/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg =
    /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}
/* 8-16位，英文数字 */
export function validateConfirmUserName(rule, value, callback) {
  if (value) {
    const zg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$/
    if (zg.test(value)) {
      if (value.length >= 6 && value.length <= 18) {
        callback()
      } else {
        callback(new Error('请输入6-18位英文字母、数字'))
      }
    } else {
      callback(new Error('请输入6-18位英文字母、数字'))
    }
  } else {
    callback(new Error('请输入账号名称'))
  }
}
/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === 'string' || str instanceof String) {
    return true
  }
  return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}

/**
 * @brief 检验密码
 * @param {String} value
 * @returns {Boolean}
 *
 */
export function validatePass(rule, value, callback) {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    if (value !== '') {
      const zg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]*$/
      if (zg.test(value)) {
        if (value.length >= 6 && value.length <= 18) {
          callback()
        } else {
          callback(new Error('请输入6-18个数字、字母组成的字符'))
        }
      } else {
        callback(new Error('请输入6-8个数字、字母组成的字符'))
      }
    }
    callback()
  }
}
/**
 * @brief 确认检验
 * @param {String} value
 * @returns {Boolean}
 *
 */
export function validateConfirmPass(rule, value, callback, confirmPass) {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== confirmPass) {
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}

// const v = new Vue()

// 文件上传前校验
/**
 *
 * @param {*} file   使用before-upload钩子时传入的文件
 * @param {*} arr    限制的扩展名
 * @param {*} mssage 错误提示
 */
// export function beforeUploadValidate(file, arr = [], mssage) {
//   const typeList = [...arr]
//   const fileName = file.name
//   const extension = fileName.substr(fileName.lastIndexOf('.') + 1)
//   const isRT = typeList.includes(extension)
//   if (!isRT) {
//     v.$message.error(mssage)
//   }
//   return isRT
// }
/**
 * @description: 校验，两个对象的值是否相等
 * @param {*}
 * @return {*}
 */
export function isObjectValueEqual(a, b) {
  let aProps = Object.getOwnPropertyNames(a)
  let bProps = Object.getOwnPropertyNames(b)

  if (aProps.length != bProps.length) {
    return false
  }

  for (let i = 0; i < aProps.length; i++) {
    let propName = aProps[i]
    let propA = a[propName]
    let propB = b[propName]
    if (propA !== propB) {
      return false
    }
  }
  return true
}

/**
 * @brief 判断是否为手机号
 * @param {String} pone
 * @returns {Boolean}
 *
 */
export function isPoneAvailable(pone) {
  const myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
  if (!myreg.test(pone)) {
    return false
  }
  return true
}

/**
 * @brief 判断是否为电话号码
 * @param {String} pone
 * @returns {Boolean}
 *
 */
export function isTelAvailable(tel) {
  // eslint-disable-next-line no-useless-escape
  const myreg = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/
  return myreg.test(tel)
}

/**
 * @brief 判断是否为中英文
 * @param {String} value
 * @returns {Boolean}
 *
 */
export function isChiEng(value) {
  // eslint-disable-next-line no-useless-escape
  const myreg = /^[\u4E00-\u9FA5A-Za-z|]*$/
  return myreg.test(value)
}
